import { Controller, Get, Post, Body } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthDto, CreateTokenDto, EmailCaptchaDto, GraphicsCaptchaDto, ParseTokenDto, VerifyTokenDto } from './dto/auth.dto';
import { ExtraCtx } from '@/common/decorators/extractx.decorator';
import { ExtraContext } from '@/common/interfaces/extracontext.interface';
import { Authority } from '@/common/decorators/authority.decorator';
import { useResult } from '@/common/hooks/useResult';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('sendGraphicsCaptcha')
  @Authority()
  async sendGraphicsCaptcha(@Body() body: GraphicsCaptchaDto, @ExtraCtx() extra: ExtraContext) {
    const res = await this.authService.sendGraphicsCaptcha(body, extra);
    return useResult(res);
  }

  @Post('sendEmailCaptcha')
  @Authority()
  async sendEmailCaptcha(@Body() body: EmailCaptchaDto, @ExtraCtx() extra: ExtraContext) {
    const res = await this.authService.sendEmailCaptcha(body, extra);
    return useResult(res);
  }

  @Post('parseToken')
  @Authority('justlogin')
  async parseToken(@Body() body: ParseTokenDto) {
    const { token } = body;
    const res = await this.authService.parseToken(token);
    return useResult(res);
  }

  @Post('refreshToken')
  @Authority()
  async refreshToken(@Body() body: VerifyTokenDto) {
    const res = await this.authService.refreshToken(body);
    return useResult(res);
  }

  @Post('parseIdentity')
  @Authority('justlogin')
  async parseIdentity(@Body() body: CreateTokenDto) {
    const res = await this.authService.parseIdentity(body);
    return useResult(res);
  }
}
